Variables in C|
A variable is the name of the memory location. It is used to store information. Its value can be altered and reused several times. It is a way to represent memory location through symbols so that it can be easily identified
Variables are key building elements of the C programming language used to store and modify data in computer programs. A variable is a designated memory region that stores a specified data type value. Each variable has a unique identifier, its name, and a data type describing the type of data it may hold.
Syntax:
The syntax for defining a variable in C is as follows: data_type variable_name;
- data_type: It represents the type of data the variable can hold. Examples of data types in C include int (integer), float (a floating-point number), char (character), double (a double-precision floating-point number),
- variable_name: It is the identifier for the variable, i.e., the name you give to the variable to access its value later in the program. The variable name must follow specific rules, like starting with a letter or underscore and consisting of letters, digits, and underscores.
For example, to declare the integer variable age: int age;
It declares an integer variable named age without assigning it a specific value. Variables can also be initialized at the time of declaration by assigning an initial value to them. For instance: int count = 0;
Here, the variable count is declared an integer and initialized with 0.Syntax:
Let us see the syntax to declare a variable: type variable_list;
An example of declaring the variable is given below: int a; float b; char c;
Here, a, b, and c are variables. The int, float, and char are the data types. We may also provide values while defining variables, as shown below: int a=20,b=30;//declaring 2 variable of integer type float f=20.9; char c='A';
Rules for defining variables
In C, Variable names must follow a few rules to be valid. The following are the rules for naming variables in C:Allowed Characters:
Variable names include letters ( uppercase and lowercase ), digits, and underscores. They must start with a letter (uppercase or lowercase) or an underscore.Case Sensitivity:
C is a case-sensitive programming language. It means that uppercase and lowercase letters are considered distinct. For example, myvar, MyVar, and myvar are all considered different variable names.Keywords:
Variable names cannot be the same as C keywords (reserved words), as they have special meanings in the language. For example, you cannot use int, float, char, for, while, etc., as variable names.Length Limitation:
There is no standard limit for the length of variable names in C, but it's best to keep them reasonably short and descriptive. Some compilers may impose a maximum length for variable names.Spaces and Special Characters:
Variable names cannot contain spaces or special characters (such as !, @, #, $, %, ^, &, *, (, ), -, +, =, [, ], {, }, |, , /, <, >,., ?;, ', or "). Underscores are the only allowed special characters in variable names.Reserved Identifiers:
While not strictly a rule, it is advisable to avoid specific patterns or identifiers common in libraries or standard usage. For example, variables starting with __ (double underscores) are typically reserved for system or compiler-specific usage. Valid examples of variable names: int age; float salary; char _status; double average_score; int studentCount; Invalid examples of variable names: int 1stNumber; // Starts with a digit float my-salary; // Contains a hyphen (-) char int; // Same as a C keyword int double; // Same as a C keyword float my$var; // Contains an unsupported special character Following these rules ensures that your variable names are valid and conform to the C language's syntax and conventions. Choosing meaningful and descriptive names for variables is essential to enhance the readability and maintainability of your code.The three components of declaring a variable
Let us explain the three aspects of defining a variable: variable declaration, variable definition, and variable initialization, along with examples.1. Variable Declaration:
The process of telling the compiler about a variable's existence and data type is known as variable declaration. It notifies the compiler that a variable with a specific name and data type will be used in the program. Still, no memory for the variable is allocated at this moment. It is usually seen at the start of a function or block before the variable is utilized.The general syntax for variable declaration is
data_type variable_name;Example of variable declaration:
#include2. Variable Definition:
The process of reserving memory space for the variable to keep its contents during program execution is known as a variable definition. It is based on the data type and connects the variable name with a particular memory address of sufficient size. A variable in C can be declared and defined in the same statement, although they can also be separated if necessary.Example of variable definition:
#include3. Variable Initialization:
Variable declaration is the act of informing the compiler about the existence and data type of a variable. It informs the compiler that a variable with a specific name and data type will be used in the program, but that memory for the variable still needs to be allocated. Not explicitly initialized variables will contain garbage/random data that may result in unexpected program behavior.Example of variable initialization:
#includeTypes of Variables in C
There are many types of variables in c:- local variable
- global variable
- static variable
- automatic variable
- external variable